package com.wyl.mine

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.alibaba.android.arouter.facade.annotation.Route
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.*
import com.wyl.base.bean.ListData
import com.wyl.base.bean.UserData
import com.wyl.base.bean.VideoData
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.dp2px
import com.wyl.libbase.extend.openActivity
import com.wyl.libbase.extend.toast
import com.wyl.mine.databinding.MineActivityBinding
import com.wyl.mine.databinding.MineHistoryItemBinding
import com.wyl.mine.setting.AboutUsActivity
import com.wyl.mine.setting.FeedbackActivity
import com.wyl.mine.setting.HelpListActivity
import com.wyl.mine.setting.SettingActivity
import com.wyl.mine.shang.ShangActivity
import com.wyl.mine.shang.TiantongActivity
import com.wyl.mine.userinfo.UserInfoActivity

@Route(path = MinePage)
class MineActivity : BindingActivity<MineActivityBinding>() {
    var mUser = ObservableField<UserData>()

    override fun getLayoutId(): Int = R.layout.mine_activity

    override fun initView() {
        binding.vm = this

        binding.titlebar.setListener { view, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                CommonTitleBar.ACTION_RIGHT_BUTTON -> openActivity(MessagePage)
                else -> {
                }
            }
        }
    }

    override fun loadData() {
        OkGo.post<BaseResponse<ListData>>(MINE_HOME)
            .params("appUserId", ACacheHelper.id)
            .converter(object : JsonConvert<BaseResponse<ListData>>() {})
            .adapt(ObservableBody<BaseResponse<ListData>>())
            .doOnSubscribe { showLoadingDialog() }
            .doFinally { dissmissLoadingDialog() }
            .map { it.data }
            .subscribe(
                { data ->
                    data.record?.let {
                        it.forEachIndexed { index, video ->
                            val itemBinding = MineHistoryItemBinding.inflate(layoutInflater, binding.history, true)
                            itemBinding.item = video
                            itemBinding.root.setOnClickListener {
                                openVideoDetailPage(video.appFolkOperaId,video.appVideoId)
                            }
                            if (index != 0) {
                                val params = itemBinding.root.layoutParams as LinearLayout.LayoutParams
                                params.marginStart =  dp2px(4.0f)
                            }
                        }
                    }
                    ACacheHelper.login(data.user!!)
                    mUser.set(data.user!!)
                }, {
                    toast(onError(it))
                }
            ).toDisposables(disposables)
    }

    fun openUserInfo() = startActivity(Intent(this, UserInfoActivity::class.java))
    fun openShang() = startActivity(Intent(this, ShangActivity::class.java))
    fun openTiantong() = startActivityForResult(Intent(this, TiantongActivity::class.java),1)
    fun openVideoHistory() = openActivity(VideoHistoryPage)
    fun openCollection() = openActivity(CollectionPage)
    fun openSetting() = openActivity(SettingActivity::class.java)
    fun openAboutUs() = openActivity(AboutUsActivity::class.java)
    fun openFeedback() = openActivity(FeedbackActivity::class.java)
    fun openHelpList() = openActivity(HelpListActivity::class.java)

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }

        if (requestCode == 1) {
            loadData()
        }

    }
}
